

//引入axios
const axios = require('axios')

//引入字体图标
require('../../libs/fonts/iconfont.css')

//引入轻提示
const t = require('../../utlis/toast')
 
require('./login.less')

document.addEventListener('DOMContentLoaded', function () {

    //获取元素
    let pswre = document.querySelector('.pswre')
    //1需求  点击密码登录  跳转到登录界面
    pswre.addEventListener('click', function () {
        location.href = './register.html'
    })



    //手机框
    const account = document.querySelector('#account')
    //    console.log(account);OK

    // 密码框
    const password = document.querySelector('#password')
    //    console.log(password);OK
    //注册框
    const btnre = document.querySelector('.btnre')
    //    console.log(btnre);

    //错误提示信息
    const errMsg = document.querySelector('.errMsg')
    //    console.log(errMsg);OK


    //正则表达式
    let phoneReg = /^1[3-9]\d{9}$/
    let PaswReg = /^\d{6,12}$/

    //表单验证
    btnre.addEventListener('click', function () {
        //    console.log(1);ok
        if (!phoneReg.test(account.value)) {
            errMsg.textContent = '手机号格式不正取'
            return;
        }

        if (!PaswReg.test(password.value)) {
            // console.log(password.value);
            errMsg.textContent = '密码格式错误'
            return;

        }
        errMsg.textContent = ''

        // 发送axios 请求
        axios.post('http://47.96.154.185:3701/api/user/login', {
            account: account.value,
            password: password.value
        })
            .then(function (res) {
                if (res.data.errno === 0) {
                    let token = res.data.data.token;
                    //  console.log(token);ok
                    localStorage.setItem('token', token)
                    t.toast('登录成功')

                    setTimeout(function () {
                     location.href = "./index.html"
                     }, 2000)

                   }
                  else {
                      t.toast('登录失败')
                 }
            })

    })




})
